const express = require('express');
const router = express.Router();
const StudentClub = require('../../models/studentClub/studentClub');

// 申请加入社团的接口
router.post('/SjoinClub', async (req, res) => {
  const { student_id, club_id } = req.body;

  if (!student_id || !club_id) {
    return res.status(400).json({ message: '缺少必要参数 student_id 或 club_id' });
  }

  try {
    // 检查是否已申请过该社团
    const existing = await StudentClub.findOne({
      where: { student_id, club_id }
    });

    if (existing) {
      return res.status(409).json({ message: 'h您已申请过该社团或已是成员' });
    }

    // 创建新的申请记录
    const newApplication = await StudentClub.create({
      student_id,
      club_id,
      status: 'pending',
      apply_time: new Date()
    });

    res.status(201).json({
      message: 'h申请成功，等待审核',
      application: newApplication
    });
  } catch (error) {
    console.error('h申请加入社团出错：', error);
    res.status(500).json({ message: 'h服务器错误，请稍后重试' });
  }
});

module.exports = router;
